Skip to content

gh-95385 Fastpath for encoding dict to JSON#95374

Merged
corona10 merged 9 commits intopython:mainfrom
aivarsk:dict_to_json_fastpath
Aug 6, 2022
Merged

gh-95385 Fastpath for encoding dict to JSON#95374
corona10 merged 9 commits intopython:mainfrom
aivarsk:dict_to_json_fastpath

Conversation

@aivarsk
Copy link
Copy Markdown
Contributor

@aivarsk aivarsk commented Jul 28, 2022

When sorting of keys is not requested and we are encoding a dict
use PyDict_Next to iterate over keys and values.
When sorting is requested use PyList_GET_ITEM instead of PyIter_Next because we know we are working with a list.

@aivarsk aivarsk force-pushed the dict_to_json_fastpath branch 2 times, most recently from 4d40abf to e2929e7 Compare July 28, 2022 13:11
@aivarsk aivarsk changed the title gh-NNNNN Fastpath for encoding dict to JSON gh-95385 Fastpath for encoding dict to JSON Jul 28, 2022
@aivarsk
Copy link
Copy Markdown
Contributor Author

aivarsk commented Jul 28, 2022

The code is based on another PR (gh-95382) not merged yet just to avoid passing extra parameters around.

@aivarsk aivarsk marked this pull request as ready for review July 28, 2022 14:30
Copy link
Copy Markdown
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate the patch from removing indent_level params.

@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

aivarsk added 2 commits July 28, 2022 23:22
When sorting of keys is not requsted and we are encoding a dict
use PyDict_Next to iterate over keys and values.
Leave the old path with PyMapping_Items that creates a list of
key-value tuples for cases when sorting is requested.
Don't use mapping items and iterator: we check and know we are
using a dict and list.
@aivarsk aivarsk force-pushed the dict_to_json_fastpath branch from 6e92af6 to 9137b1b Compare July 28, 2022 20:23
@aivarsk
Copy link
Copy Markdown
Contributor Author

aivarsk commented Jul 28, 2022

I have made the requested changes; please review again

@bedevere-bot
Copy link
Copy Markdown

Thanks for making the requested changes!

@corona10: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from corona10 July 28, 2022 20:25
Copy link
Copy Markdown
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like to provide a benchmark script and result for the following conditions?

  • json.dumps when sorting of key is required
  • json.dumps when sorting of key is not required (your suggestion)

@aivarsk
Copy link
Copy Markdown
Contributor Author

aivarsk commented Jul 29, 2022

This is without sorting of keys, the default behavior of json.dumps
pyperformance on my machine before changes:

### json_dumps ###
Mean +- std dev: 10.6 ms +- 0.1 ms

pyperformance after changes:

### json_dumps ###
Mean +- std dev: 9.00 ms +- 0.08 ms

@aivarsk
Copy link
Copy Markdown
Contributor Author

aivarsk commented Jul 29, 2022

With pyperformance sort_keys=True, I edited benchmarks/bm_json_dumps/run_benchmark.py for that

before changes

### json_dumps ###
Mean +- std dev: 14.0 ms +- 0.2 ms

after changes (PyList_GET_ITEM is a bit faster than PyIter_Next?)

### json_dumps ###
Mean +- std dev: 13.6 ms +- 0.2 ms

Comment thread Modules/_json.c
@corona10
Copy link
Copy Markdown
Member

@aivarsk Thank you, I will take a look at this PR through this weekend :)

@corona10 corona10 self-assigned this Jul 29, 2022
@mdboom mdboom added the performance Performance or resource usage label Aug 1, 2022
@corona10 corona10 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 3, 2022
@bedevere-bot
Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @corona10 for commit 9137b1b 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 3, 2022
Comment thread Modules/_json.c
Comment thread Modules/_json.c Outdated
Comment thread Modules/_json.c Outdated
@aivarsk aivarsk requested a review from corona10 August 4, 2022 09:43
Comment thread Modules/_json.c Outdated
Comment thread Modules/_json.c
Copy link
Copy Markdown
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, I should check several possibilities of regression including leaks but not found :)

Overall looks good LGTM, but please follow PEP 7 if possible.
I just left nit comments :)

Comment thread Modules/_json.c Outdated
Comment thread Modules/_json.c Outdated
Comment thread Modules/_json.c Outdated
Comment thread Modules/_json.c Outdated
aivarsk and others added 4 commits August 5, 2022 23:03
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
@corona10 corona10 merged commit 15f4a35 into python:main Aug 6, 2022
iritkatriel pushed a commit to iritkatriel/cpython that referenced this pull request Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants